// pages/xiadan/xiadan.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jihua: [],
    jihuaall: [],
    jihua2: [],
    jihua3: [],
    color1: "#ffffff",
    color2: "#ffffff",
    color3: "#ffffff",
    openid: '',
    city:"",
    weather:'',
    temperature:''
  },

  details: function (e) {
    console.log("details", e)
    console.log("detailsid", e.currentTarget.dataset.id)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/details/details?id=' + id,
    })
  },

  xinzeng: function () {
    wx.navigateTo({
      url: '/pages/xinzeng/xinzeng',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showToast({
      title: '加载中',
      icon:'loading',
      duration:2000
    })
    var that=this;
    //获取用户定位
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const accuracy = res.accuracy
        var dizhi = longitude+","+latitude
        console.log(dizhi)
        console.log(longitude,latitude)
        wx.request({
          url: 'https://restapi.amap.com/v3/geocode/regeo?parameters', //仅为示例，并非真实的接口地址
          data: {
            location: dizhi,
            key:'678a888f10d1b680e496b1b14cac9b38'
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data)
            console.log(res.data.regeocode.addressComponent.adcode)
            var city = res.data.regeocode.addressComponent.adcode
            wx.request({
              url: 'https://restapi.amap.com/v3/weather/weatherInfo?parameters',
              data: {
                city: city,
                key:'678a888f10d1b680e496b1b14cac9b38'
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success (res) {
                console.log("天气",res.data)
                console.log("气温",res.data.lives[0].temperature)
                console.log("气候",res.data.lives[0].weather)
                console.log("城市",res.data.lives[0].city)
                var weather = res.data.lives[0].weather
                var temperature = res.data.lives[0].temperature
                var city = res.data.lives[0].city
                that.setData({
                  weather:weather,
                  temperature:temperature,
                  city:city
                })
              }
            })
          }
        })
        
      },
      fail(){
        wx.showModal({
          title: '提示',
          content: '此功能需获取位置信息，请授权',
          success: function (res) {
            if (res.confirm == false) {
              return false;
            }
            wx.openSetting({
              success(res) {
                //如果再次拒绝则返回页面并提示
                if (!res.authSetting['scope.userLocation']) {
                  wx.showToast({
                    title: '此功能需获取位置信息，请重新设置',
                    duration: 3000,
                    icon: 'none'
                  })
                } else {
                  //允许授权，调用地图
                  that.onLoad()
                }
              }
            })
          }
        }) 
    }
     })
     
  },

  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ', res.result.openid)
        var openid = res.result.openid;
        that.setData({
          openid: openid
        })
        var openid2 = this.data.openid
        console.log('2', openid2)
        var jihua = []
        db.collection("jihua").where({
          use: this.data.openid,
          finish: false
        }).orderBy('nowtime', 'asc').limit(30).get({
          success: res => {
            for (var i = 0; i < res.data.length; i++) {
              jihua.push(res.data[i])
            }
            db.collection("jihua").where({
              _openid: openid2,
              finish: false
            }).orderBy('nowtime', 'asc').limit(30).get({
              success: res => {
                for (var i = 0; i < res.data.length; i++) {
                  jihua.push(res.data[i])
                }
                this.setData({
                  jihua: jihua
                })
                console.log("789", this.data.jihua)
              }
            })
          }
        })

        console.log("123", this.data.openid)
      }
    })
  },

//下拉刷新
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOpenid();
    wx.stopPullDownRefresh()
    
    // db.collection("jihua").where({
    //   use:this.data.openid
    // }).get({
    //   success: res => {
    //     this.setData({
    //       jihuaall: res.data
    //     })
    //     console.log(this.data.jihua)
    //   }
    // })
    // db.collection("jihua2").get({
    //   success:res=>{
    //     this.setData({
    //       jihua2:res.data
    //     })
    //     console.log(this.data.jihua2)
    //   }
    // })
    // db.collection("jihua3").get({
    //   success:res=>{
    //     this.setData({
    //       jihua3:res.data
    //     })
    //     console.log(this.data.jihua3)
    //   }
    // })
    // db.collection("jihua").get({
    //   success:res=>{
    //     this.setData({
    //       color1:"#D6F4FF"
    //     })
    //     console.log(this.data.color3)
    //   }
    // })
    // db.collection("jihua2").get({
    //   success:res=>{
    //     this.setData({
    //       color2:"#E3E0F3"
    //     })
    //     console.log(this.data.color3)
    //   }
    // })
    // db.collection("jihua3").get({
    //   success:res=>{
    //     this.setData({
    //       color3:"#C1EBE0"
    //     })
    //     console.log(this.data.color3)
    //   }
    // })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})